﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace 三国杀
{
    public partial class QQLogin : Form
    {
        public QQLogin()
        {
            InitializeComponent();
            this.MouseDown += FrmQQLogin_MouseDown;
            this.MouseUp += FrmQQLogin_MouseUp;
            this.MouseMove += FrmQQLogin_MouseMove;
            LoadCredentials();
        }

        Point mousePosition;

        Point formPosition;

        bool isMouseDown = false;//是否按下


        private void FrmQQLogin_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;

            mousePosition = Control.MousePosition;  //鼠标位置

            formPosition = this.Location;   //窗体左上角位置
        }

        private void FrmQQLogin_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void FrmQQLogin_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePoint = Control.MousePosition;   //移动的位置
            if (isMouseDown)
            {
                this.Location = new Point(mousePoint.X - mousePosition.X + formPosition.X, mousePoint.Y - mousePosition.Y + formPosition.Y);
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //最小化按钮
        private void PictureBox1_Click(object sender, EventArgs e)
        {
            //最小化窗口
            this.WindowState = FormWindowState.Minimized;
        }

        //注册按钮
        // 注册按钮
        private void PictureBox2_Click(object sender, EventArgs e)
        {
            // 保存当前窗口位置
            Point originalLocation = this.Location;
            this.Hide(); // 先隐藏登录窗口

            using (frmRegister registerForm = new frmRegister())
            {
                // 设置注册窗口位置与登录窗口相同
                registerForm.StartPosition = FormStartPosition.Manual;
                registerForm.Location = originalLocation;

                // 显示注册窗口(模态)
                if (registerForm.ShowDialog() == DialogResult.OK)
                {
                    // 确保QQNumber不为空
                    if (!string.IsNullOrEmpty(registerForm.QQNumber))
                    {
                        this.txtzhanghao.Text = registerForm.QQNumber;
                        this.txtpassword.Focus();
                    }
                }

                // 无论注册成功与否，都重新显示登录窗口
                this.Location = originalLocation;
                this.Show();
            }
        }

        //登录按钮
        private void PictureBox3_Click(object sender, EventArgs e)
        {
            Login();
            SaveCredentials();
        }

        //找回密码按钮
        private void Button1_Click(object sender, EventArgs e)
        {
            new Loading().Show();                               //显示加载窗体
            var QQFindPwd = new QQFindPwd();                    //创建QQMain窗体对象
            QQFindPwd.Show();                                   //显示QQFindPwd窗体
            this.Hide();
        }


        //自动登录按钮
        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.CheckBox1.Checked)
            {
                // 如果勾选自动登录，同时勾选记住密码
                this.CheckBox2.Checked = true;
            }
        }

        //记住密码按钮
        private void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.CheckBox2.Checked)
            {
                // 如果取消记住密码，同时取消自动登录
                this.CheckBox1.Checked = false;
            }
        }

        DB db = new DB();//数据库对象

        public void SetQQNumber(string qqNumber)
        {
            txtzhanghao.Text = qqNumber;
        }

        public void AutoLogin()
        {
            db.dbOpen();                                            //打开数据库连接
            string sql = "select count(*) from QQUsers where QQ='" + this.txtzhanghao.Text + "' and Pwd='" + this.txtpassword.Text + "'";   //查询数据库是否有该用户
            SqlCommand comm = new SqlCommand(sql, db.Conn);         //创建SqlCommand对象
            int num = (int)comm.ExecuteScalar();                    //执行查询语句，并返回结果
            db.dbClose();                                           //关闭数据库连接
            if (num == 1)
            {
                new Loading().Show();                               //显示加载窗体
                var QQMain = new QQMain();                          //创建QQMain窗体对象
                QQMain.Userid = this.txtzhanghao.Text;              //将QQ账号和密码赋值给QQMain窗体对象
                QQMain.password = this.txtpassword.Text;
                //SaveCredentials();                                  //保存账号和密码到本地文件
                QQMain.Show();                                      //显示QQMain窗体
                this.Hide();
            }
            else
            {
                MessageBox.Show("账号或密码错误，登录失败");
            }
        }

        //登录按钮事件
        public void Login()
        {
            if (this.txtzhanghao.Text != "")
            {
                if (this.txtpassword.Text != "")
                {
                    db.dbOpen();
                    string sql = "select count(*) from QQUsers where QQ='" + this.txtzhanghao.Text + "' and Pwd='" + this.txtpassword.Text + "'";
                    SqlCommand comm = new SqlCommand(sql, db.Conn);
                    int num = (int)comm.ExecuteScalar();
                    if (num == 1)
                    {
                        new Loading().Show();
                        QQMain QQMain = new QQMain();
                        QQMain.Userid = this.txtzhanghao.Text;
                        QQMain.password = this.txtpassword.Text;
                    
                        QQMain.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("账号或密码错误，登录失败");
                    }
                }
                else
                {
                    MessageBox.Show("QQ密码不能为空请重新输入");
                    this.txtpassword.Focus();
                }
            }
            else
            {
                MessageBox.Show("QQ账号不能为空请重新输入");
                this.txtzhanghao.Focus();
            }
        }
        // 保存账号和密码到本地文件
        private void SaveCredentials()
        {
            // 如果勾选记住密码，保存账号和密码到本地文件
            if (this.CheckBox2.Checked)
            {
                string filePath = Path.Combine(Application.StartupPath, "credentials.txt");
                try
                {
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        writer.WriteLine(this.CheckBox1.Checked);
                        writer.WriteLine(this.txtzhanghao.Text);
                        writer.WriteLine(this.txtpassword.Text);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"保存凭据时出错: {ex.Message}");
                }
            }
            else
            {
                // 如果不勾选记住密码，清空文件
                string filePath = Path.Combine(Application.StartupPath, "credentials.txt");
                if (File.Exists(filePath))
                {
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"删除凭据文件时出错: {ex.Message}");
                    }
                }
            }
        }

        // 从本地文件加载账号和密码
        private void LoadCredentials()
        {
            string filePath = Path.Combine(Application.StartupPath, "credentials.txt");
            if (File.Exists(filePath))
            {
                try
                {
                    using (StreamReader reader = new StreamReader(filePath))
                    {
                        bool autoLogin = bool.Parse(reader.ReadLine());
                        this.txtzhanghao.Text = reader.ReadLine();
                        this.txtpassword.Text = reader.ReadLine();
                        this.CheckBox1.Checked = autoLogin;
                        this.CheckBox2.Checked = true;
                        if (autoLogin)
                        {
                            AutoLogin();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"加载凭据时出错: {ex.Message}");
                }
            }
        }

        private void QQLogin_Load(object sender, EventArgs e)
        {

        }
    }
}